#include "usermgr.h"
#include "global.h"
UserMgr::UserMgr()
    : _chat_loaded(0)
    , _contact_loaded(0)
{}

int UserMgr::GetUid()
{
    return _userInfo->uid;
}

void UserMgr::SetUserInfo(std::shared_ptr<UserInfo> userInfo)
{
    _userInfo = userInfo;
}

std::shared_ptr<UserInfo> UserMgr::GetUserInfo()
{
    return _userInfo;
}

QVector<std::shared_ptr<ApplyInfo> > UserMgr::GetApplyList()
{
    return _applyList;
}

bool UserMgr::AlreadyApply(int uid)
{
    for (auto &item : _applyList) {
        if (item->uid == uid) {
            return true;
        }
    }
    return false;
}

void UserMgr::AddApplyList(QJsonArray array)
{
    for (const QJsonValue &value : array) {
        std::shared_ptr<ApplyInfo> applyInfo = std::make_shared<ApplyInfo>();
        applyInfo->name = value["name"].toString();
        applyInfo->desc = value["desc"].toString();
        applyInfo->uid = value["from_uid"].toInt();
        applyInfo->email = value["email"].toString();
        applyInfo->status = value["status"].toInt();
        applyInfo->sex = value["sex"].toInt();
        _applyList.push_back(applyInfo);
    }
}

void UserMgr::AddApplyInfo(std::shared_ptr<ApplyInfo> applyInfo)
{
    _applyList.push_back(applyInfo);
}

void UserMgr::AddFriend(std::shared_ptr<FriendInfo> friendInfo)
{
    if (!friendInfo)
        return;
    int uid = friendInfo->uid;
    if (FriendExists(uid))
        return;
    _friends[uid] = friendInfo;
}

bool UserMgr::FriendExists(int uid)
{
    auto iter = _friends.find(uid);
    if (iter == _friends.end()) {
        return false;
    }
    return true;
}

void UserMgr::AddFriendList(QJsonArray array)
{
    // 添加好友列表
    for (const QJsonValue &value : array) {
        std::shared_ptr<FriendInfo> friendInfo = std::make_shared<FriendInfo>();
        friendInfo->name = value["name"].toString();
        friendInfo->desc = value["desc"].toString();
        friendInfo->uid = value["uid"].toInt();
        friendInfo->email = value["email"].toString();
        friendInfo->remark = value["remark"].toString();
        friendInfo->sex = value["sex"].toInt();
        friendInfo->icon = value["icon"].toString();
        _friends[friendInfo->uid] = friendInfo;
        _friendList.push_back(friendInfo);
    }
}

QVector<std::shared_ptr<FriendInfo>> &UserMgr::GetFriendList()
{
    return _friendList;
}

QVector<std::shared_ptr<FriendInfo>> UserMgr::GetChatListPerPage()
{
    QVector<std::shared_ptr<FriendInfo>> friend_list;
    unsigned int begin = _chat_loaded;
    unsigned int end = begin + CHAT_COUNT_PER_PAGE;
    //
    if (begin >= _friendList.size()) {
        return friend_list;
    }
    if (end > _friendList.size()) {
        friend_list = QVector<std::shared_ptr<FriendInfo>>(_friendList.begin() + begin,
                                                           _friendList.end());
        return friend_list;
    }
    friend_list = QVector<std::shared_ptr<FriendInfo>>(_friendList.begin() + begin,
                                                       _friendList.begin() + end);
    return friend_list;
}

bool UserMgr::IsLoadChatFinish()
{
    return _chat_loaded > _friendList.size();
}

void UserMgr::UpdateChatLoadedCount()
{
    unsigned int begin = _chat_loaded;
    unsigned int end = begin + CHAT_COUNT_PER_PAGE;
    if (begin >= _friendList.size()) {
        return;
    }
    if (end > _friendList.size()) {
        _chat_loaded = _friendList.size();
        return;
    }
    _chat_loaded = end;
}

QVector<std::shared_ptr<FriendInfo>> UserMgr::GetContactListPerPage()
{
    QVector<std::shared_ptr<FriendInfo>> friend_list;
    unsigned int begin = _contact_loaded;
    unsigned int end = begin + CONTACT_COUNT_PER_PAGE;

    if (begin >= _friendList.size()) {
        return friend_list;
    }
    if (end > _friendList.size()) {
        friend_list = QVector<std::shared_ptr<FriendInfo>>(_friendList.begin() + begin,
                                                           _friendList.end());
        return friend_list;
    }
    friend_list = QVector<std::shared_ptr<FriendInfo>>(_friendList.begin() + begin,
                                                       _friendList.begin() + end);
    return friend_list;
}

bool UserMgr::IsLoadContactFinish()
{
    return _contact_loaded > _friendList.size();
}

void UserMgr::UpdateContactLoadedCount()
{
    unsigned int begin = _contact_loaded;
    unsigned int end = begin + CHAT_COUNT_PER_PAGE;
    if (begin >= _friendList.size()) {
        return;
    }
    if (end > _friendList.size()) {
        _contact_loaded = _friendList.size();
        return;
    }
    _contact_loaded = end;
}

std::shared_ptr<FriendInfo> UserMgr::GetFriend(int uid)
{
    auto iter = _friends.find(uid);
    if (iter == _friends.end()) {
        return nullptr;
    }
    return iter.value();
}

void UserMgr::AddMessageList(QJsonArray array, QVector<QString> &msg_id_list)
{
    // 添加未收到的消息
    for (const QJsonValue &message : array) {
        std::shared_ptr<TextChatData> textChatData = std::make_shared<TextChatData>();
        textChatData->from_uid = message["fromuid"].toInt();
        textChatData->to_uid = message["touid"].toInt();
        textChatData->msg_id = message["msg_id"].toString();
        textChatData->msg_content = message["message"].toString();
        textChatData->send_time = message["send_time"].toString();
        msg_id_list.push_back(textChatData->msg_id);
        _unreceived_message_list.push_back(textChatData);
    }
}

QVector<std::shared_ptr<TextChatData>> &UserMgr::GetUnreceivedMessageList()
{
    return _unreceived_message_list;
}

UserMgr::~UserMgr() {}
